home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18030 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  101 lines

  1. Path: utopia.hacktic.nl!not-for-mail
  2. From: Mike Tavares <MIKET@cdynamics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: String operator+ and memory leakage.
  5. Date: 18 Apr 1996 15:23:36 +0200
  6. Organization: Hack-Tic International, Inc.
  7. Sender: remailer@utopia.hacktic.nl
  8. Message-ID: <4l5foo$fen@utopia.hacktic.nl>
  9. NNTP-Posting-Host: utopia.hacktic.nl
  10. Content-Type: text/plain; charset=US-ASCII
  11. Content-Transfer-Encoding: 7bit
  12. Comment: This message was forwarded by an automated remailing service.
  13. Comment: No attempt was made to verify the sender's identity.
  14. Comment: Please report misuse to <postmaster@utopia.hacktic.nl>
  15.  
  16. I have an implementation (not mine) of the String class that is leaking 
  17. memory
  18. during the + operator code.  The code follows:
  19.  
  20. class String
  21.     {
  22.     public:
  23.  
  24.         // Constructors/destructors
  25.         String();
  26.         String( char* new_string);
  27.         String( String& new_string);
  28.  
  29.         ~String()
  30.             {
  31.             delete character_array;
  32.             }
  33.  
  34.         String&     operator+( String& a_string );
  35.         String&     operator=( String& a_string );
  36.         operator    char* () { return character_array; }
  37.  
  38.     private:
  39.  
  40.         char* character_array;
  41.         int string_length;
  42.     };
  43.  
  44. String&
  45. String::operator+( String& a_string )
  46.     {
  47.     int total_size = string_length + strlen( a_string ) + 1;
  48.     char* temp_array = new char[total_size];
  49.     char* char_array = a_string;
  50.  
  51.     strcpy( temp_array, character_array );
  52.     strcat( temp_array, char_array );
  53.  
  54.     String* new_string = new String( temp_array );
  55.     delete [] temp_array;
  56.     return *new_string;
  57.     }
  58.  
  59. String&
  60. String::operator=( String& a_string )
  61.     {
  62.     char* new_char_array = a_string;
  63.     delete [] character_array;
  64.     character_array = new char[strlen( new_char_array ) + 1];
  65.     strcpy( character_array, new_char_array );
  66.     string_length = strlen( character_array );
  67.     return *this;
  68.     }
  69.  
  70. My usage is:
  71.  
  72. destString = string1 + string2 + string3...;
  73.  
  74.  
  75. The problem is, when new_string is created and returned through a 
  76. reference, no one ever deletes it.
  77.  
  78. If I change new_string to be an automatic variable I get an error from 
  79. the compiler about trying to pass a reference to a item that is out of 
  80. scope.
  81.  
  82. If I change the method to work in this I mutate one of my addends.  There 
  83. has to be a way of implementing this without memory leakage! HELP!
  84.  
  85. TIA
  86.  
  87. =MikeT
  88.  
  89.  ---------------------------------------------------------------------
  90. | Mike Tavares         | miket@cdynamics.com    |                     |
  91. | Software Engineer    |                        | If you're not lead  |
  92. |      -----           |      -----             | dog the view never  |
  93. | Computer Dynamics    | (803) 627-8800         | changes.            |
  94. | 7640 Pelham Rd       |       EXT - 232        |                     |
  95. | Greenville, SC 29615 | Fax. (803) 675-0106    |   Email for PGP key |
  96. |==================================================================== |
  97. |            Computer Dynamics - The Flat Panel Experts               |
  98.  ---------------------------------------------------------------------
  99.  
  100.  
  101.